<?php
/*******************************************************************************
Rd/Displayer/Admin.php

Created by Jason White (jbwhite@emory.edu)
Modified by NCSU Libraries, NC State University. Modifications by Troy Hurteau (libraries.opensource@ncsu.edu).

This file is part of NCSU's distribution of ReservesDirect. This version has not been downloaded from Emory University
or the original developers of ReservesDirect. Neither Emory University nor the original developers of ReservesDirect have authorized
or otherwise endorsed or approved this distribution of the software.

Copyright (c) 2004-2006 Emory University, Atlanta, Georgia.

Licensed under the NCSU ReservesDirect License, Version 2.0 (the "License"); 
you may not use this file except in compliance with the License. You may obtain a copy of the full License at
 http://www.lib.ncsu.edu/it/opensource/

ReservesDirect is distributed in the hope that it will be useful,
but is distributed "AS IS" and WITHOUT ANY WARRANTY, without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE, and without any warranty as to non-infringement of any third
party's rights. See the License for the specific language governing permissions and limitations under the License.

The original version of ReservesDirect is located at:
http://www.reservesdirect.org/

This version of ReservesDirect, distributed by NCSU, is located at:
http://code.google.com/p/reservesdirect-ncsu/

*******************************************************************************/
require_once(APPLICATION_PATH . '/common.inc.php');
require_once(APPLICATION_PATH . '/classes/terms.class.php');
require_once(APPLICATION_PATH . '/displayers/helpDisplayer.class.php');
require_once(APPLICATION_PATH . '/lib/Rd/Displayer/Base.php');

require_once(APPLICATION_PATH . '/lib/Service/Key.php');
require_once(APPLICATION_PATH . '/lib/Filter/DelimiterExpand.php');

class Rd_Displayer_Admin extends Rd_Displayer_Base 
{

	protected $_displayerName = 'admin';
	
	/**
	 * Display List all admins available to user
	 *
	 * $adminList array adminID, adminTitle
	 */
	function displayAdminFunctions($model = array())
	{
		$g_siteURL = Rd_Registry::get('root:mainUrl');
		$model['rdUrl'] = $g_siteURL;
		$model['primeServiceKey'] = Service_Key::getPrimary();
		$this->display('index', $model);
	}
	
	function displayEditDept($function, $libraries)
	{		
		?>
		<script language="JavaScript1.2" src="public/javascript/liveSearch.js"></script>
		<script language="JavaScript1.2">
			function deptnameReturnAction(dept_array)
			{
				eval("var dept = " + decode64(dept_array));	
					
				document.getElementById('editDeptArea').style.display = '';	

				if (typeof(dept['department_id']) != "undefined")
				{
					document.getElementById('dept_id').value 	= dept['department_id'];
					document.getElementById('dept_name').value 	= dept['name'];
					document.getElementById('dept_abbr').value 	= dept['abbreviation'];
					
					var lib = document.getElementById('library_id');
					
					for(var i=0; i<lib.options.length; i++)
						if (lib.options[i].value == dept['library_id'])
							lib.selectedIndex = i;
					
					document.getElementById('dept_name').enabled = true;
					document.getElementById('dept_abbr').enabled = true;
					document.getElementById('library_id').enabled = true;
					
					document.getElementById('dept_name').focus();
				}
				
			}
			
			function checkSubmit()
			{
				if (document.getElementById('dept_name').value == "")
				{
					alert ("Department Name is required.");
					return false;
				}
				
				if (document.getElementById('dept_abbr').value == "")
				{
					alert ("Department Abbreviation is required.");
					return false;
				}				
				
				if (document.getElementById('library_id').selectedIndex == -1)
				{
					alert ("Processing Library is required.");
					return false;
				}
			}
			
			function createNew(deptName){
				document.getElementById('editDeptArea').style.display = '';	
				
				document.getElementById('dept_id').enabled = false;
				document.getElementById('dept_name').value 	= deptName;
				document.getElementById('dept_abbr').value 	= '';
				document.getElementById('library_id').selectedIndex = -1;
					
				document.getElementById('dept_name').enabled = true;
				document.getElementById('dept_abbr').enabled = true;
				document.getElementById('library_id').enabled = true;
				
				document.getElementById('dept_name').focus();
			}
		</script>			
		
		<table>
			<tr><td width="5"></td><td><input type="button" onClick="createNew('');" value="New Department"></td></tr>
			<tr><td width="5"></td><td class="strong">Enter Department Name or Abbreviation:</td></tr>
			<tr><td width="5"></td>
		
				<td>					
					<input size=55 name="dept_search" id="dept_search" value="" onKeyPress="liveSearchStart(event, this, 'AJAX_functions.php?f=deptList', document.getElementById('LSResult'), 'deptnameReturnAction');">
					<div class="LSResult" id="LSResult" style="display: none;"><ul id="LSShadow"></ul></div>
					<script language="JavaScript">liveSearchInit(document.getElementById("dept_search"));</script>
				</td>
		
			</tr>
		<form method="POST" action="index.php?cmd=admin&function=saveDept" onSubmit="return checkSubmit();">
		<input type="hidden" id="dept_id" name="dept_id" value="">			

			<tr><td></td><td>&nbsp;</td></tr>

			<table id="editDeptArea" style="display: none;">
				<tr>
					<td>&nbsp;</td>
					<td>Department Name:</td>
					<td><input id="dept_name" name="dept_name" value="" size="55"></td>
				</tr>			
				
				<tr>
					<td>&nbsp;</td>
					<td>Department Abbreviation:</td>
					<td><input id="dept_abbr" name="dept_abbr" value="" size="55"></td>
				</tr>
				
				<tr>
					<td>&nbsp;</td>
					<td>Processing Library:</td>
					<td>
						<select name="library_id" id="library_id">
						<?php
							foreach ($libraries as $l)
							{
								echo "<option value=\"" . $l->getLibraryID() ."\">". $l->getLibraryNickname() . "</option>";
							}
						?>
						</select>
					</td>
				</tr>
			</table>
			
		</table>		
		
		<tr><td>&nbsp;</td><td>&nbsp;</td></tr>
		<tr><td>&nbsp;</td><td><input type=submit value="Save Department" id="frmSubmit"></td></tr>
		
		</form>
		<?php
	}

	function displayEditLibrary($libraries)
	{		
		?>
		<script language="JavaScript1.2" src="public/javascript/liveSearch.js"></script>
		<script language="JavaScript1.2">
			function getLibraryData(element)
			{
				value = element.options[element.selectedIndex].value;
				if (liveSearchLast != value)
				{					
					liveSearchInitXMLHttpRequest();
					
					liveSearchReq.onreadystatechange = libraryReturnAction;
					liveSearchReq.open("GET", 'AJAX_functions.php?f=libList&qu=' + encode64(value) + "&rf=" + encode64('libraryReturnAction'));
					liveSearchLast = value;
			
					liveSearchReq.send(null);
				}
			}
		
			function libraryReturnAction(event, lib_array)
			{				
				if (liveSearchReq.readyState == 4) {
					var decodedValue = decode64(liveSearchReq.responseText);	
					eval("var lib = " + decodedValue);	
					
					document.getElementById('lib_id').value 			= lib['id'];
					document.getElementById('lib_name').value 			= lib['name'];
					document.getElementById('lib_nickname').value 		= lib['nickname'];
					document.getElementById('ils_prefix').value 		= lib['ils_prefix'];										
					document.getElementById('desk').value 				= lib['desk'];
					document.getElementById('lib_url').value		 	= lib['url'];		
					document.getElementById('contactEmail').value 	= lib['email'];			
					
					var mono = document.getElementById('monograph_library_id');												
					for(var i=0; i<mono.options.length; i++)
						if (mono.options[i].value == lib['monograph_library_id'])
							mono.selectedIndex = i;
					
					var multi = document.getElementById('multimedia_library_id');						
					for(var i=0; i<multi.options.length; i++)
						if (multi.options[i].value == lib['multimedia_library_id'])
							multi.selectedIndex = i;
							
					document.getElementById('lib_id').enabled = true;
					document.getElementById('lib_name').enabled = true;
					document.getElementById('lib_nickname').enabled = true;
					document.getElementById('ils_prefix').enabled = true;
					document.getElementById('desk').enabled = true;
					document.getElementById('lib_url').enabled = true;
					document.getElementById('contactEmail').enabled = true;
					document.getElementById('monograph_library_id').enabled = true;
					document.getElementById('multimedia_library_id').enabled = true;
					
					document.getElementById('lib_name').focus();
				}				
			}
			
			function checkSubmit()
			{
				if (document.getElementById('lib_name').value == "")
				{
					alert ("library Name is required.");
					return false;
				}
				
				if (document.getElementById('lib_nickname').value == "")
				{
					alert ("library nickname is required.");
					return false;
				}				
				
				if (document.getElementById('monograph_library_id').selectedIndex == -1)
				{
					alert ("Monograph Processing Library is required.");
					return false;
				}
				if (document.getElementById('multimedia_library_id').selectedIndex == -1)
				{
					alert ("Multimedia Processing Library is required.");
					return false;
				}				

			}
			
			function createNew()
			{
				document.getElementById('lib_id').value 			= "";
				document.getElementById('lib_name').value 			= "";
				document.getElementById('lib_nickname').value 		= "";
				document.getElementById('ils_prefix').value 		= "";
				document.getElementById('desk').value 				= "";
				document.getElementById('lib_url').value		 	= "";
				document.getElementById('contactEmail').value 		= "";
				
				document.getElementById('monograph_library_id').selectedIndex = -1;
				document.getElementById('multimedia_library_id').selectedIndex = -1;
					
				document.getElementById('lib_id').enabled = false;
				document.getElementById('lib_name').enabled = true;
				document.getElementById('lib_nickname').enabled = true;
				document.getElementById('ils_prefix').enabled = true;
				document.getElementById('desk').enabled = true;
				document.getElementById('lib_url').enabled = true;
				document.getElementById('contactEmail').enabled = true;
				document.getElementById('monograph_library_id').enabled = true;
				document.getElementById('multimedia_library_id').enabled = true;
				
				document.getElementById('lib_name').focus();
			}
		</script>			
		
		
		<table>
			<tr><td width="5"></td><td class="strong">Enter library Name or nickname:</td></tr>
			<tr><td width="5"></td>
		
				<td>					
						<select name="lib_search" id="lib_search" onChange="getLibraryData(this);" >
						<?php
							foreach ($libraries as $l)
							{
								echo "<option value=\"" . $l->getLibraryID() ."\">". $l->getLibraryNickname() . "</option>";
							}
						?>
						</select>
						&nbsp;
						<!--<input type="button" value="Lookup Library" onClick="getLibraryData(document.getElementById('lib_search'));">-->
						&nbsp;
						<input type="button" value="Create New Library" onClick="createNew();">
					<div class="LSResult" id="LSResult1" style="display: none;"><ul id="LSShadow"></ul></div>					
				</td>
		
			</tr>
		<form method="POST" action="index.php?cmd=admin&function=saveLib" onSubmit="return checkSubmit();">
		<input type="hidden" id="lib_id" name="lib_id" value="">			
			<tr><td></td><td>&nbsp;</td></tr>
			
			<table id="editlibArea">
				<tr>
					<td>&nbsp;</td>
					<td>Library Name:</td>
					<td><input id="lib_name" name="lib_name" value="" size="112" maxlength="100"></td>
				</tr>			
				
				<tr>
					<td>&nbsp;</td>
					<td>Library Nickname:</td>
					<td><input id="lib_nickname" name="lib_nickname" value="" size="20" maxlength="15"></td>
				</tr>
				
				<tr>
					<td>&nbsp;</td>
					<td>ILS Prefix:</td>
					<td><input id="ils_prefix" name="ils_prefix" value="" size="12" maxlength="10"></td>
				</tr>

				<tr>
					<td>&nbsp;</td>
					<td>Reserve Desk:</td>
					<td><input id="desk" name="desk" value="" size="55" maxlength="50"></td>
				</tr>				
				
				<tr>
					<td>&nbsp;</td>
					<td>URL:</td>
					<td><input id="lib_url" name="lib_url" value="" size="112"></td>
				</tr>				
				
				<tr>
					<td>&nbsp;</td>
					<td>Contact Email:</td>
					<td><input id="contactEmail" name="contactEmail" value="" size="112" maxlength="255"></td>
				</tr>				
				
				<tr>
					<td>&nbsp;</td>
					<td>Monograph Processing Library:</td>
					<td>
						<select name="monograph_library_id" id="monograph_library_id">
							<!--  <option value="null">No Monograph Processing Library</option> -->
						<?php					
							foreach ($libraries as $l)
							{
								echo "<option value=\"" . $l->getLibraryID() ."\">". $l->getLibraryNickname() . "</option>";
							}
						?>
						</select>
					</td>
				</tr>
				
				<tr>
					<td>&nbsp;</td>
					<td>Multimedia Processing Library:</td>
					<td>
						<select name="multimedia_library_id" id="multimedia_library_id">
							<!--  <option value="null">No Multimedia Processing Library</option> -->
						<?php
							foreach ($libraries as $l)
							{
								echo "<option value=\"" . $l->getLibraryID() ."\">". $l->getLibraryNickname() . "</option>";
							}
						?>
						</select>
					</td>
				</tr>				
			</table>
			
		</table>		
		
		<tr><td>&nbsp;</td><td>&nbsp;</td></tr>
		<tr><td>&nbsp;</td><td><input type=submit value="Save library" id="frmSubmit"></td></tr>
		
		</form>
		<script language="JavaScript">getLibraryData(document.getElementById('lib_search'));</script>
		<?php
	}
		
	/**
	 * @return void
	 * @param string $next_cmd The next command to execute
	 * @param array $courses (optional) Array of cross-listings
	 * @param string $msg (optional) Text to display above the class select
	 * @param array $hidden_fields (optional) Array of info to pass on as hidden fields
	 * @param string $return_variable name of html variable for selected
	 * @desc Displays list of courses (course_alias) for selection
	 */
	public function displayEditRegistrarKey($next_cmd, $courses=null, $msg=null, $hidden_fields=null, $return_variable='ca') 	
	{

		if(!empty($msg)) {
			echo "<span class=\"helperText\">$msg</span><p />\n";
		}
		
		print("<form action=\"index.php\" method=\"post\" name=\"select_course\" id=\"select_class\">\n");
		print("	<input type=\"hidden\" id=\"cmd\" name=\"cmd\" value=\"$next_cmd\" />\n");
		
		self::displayHiddenFields($hidden_fields);

		$c = new course();
		
		print "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\" align=\"center\">\n";
		
		$rowNumber = 0;

		print("<td></td><td></td><td></td></td><td style='text-align:center'>Override All&nbsp;&nbsp; <input type='checkbox' id='overrideAll'></td>");
		foreach($courses as $c)
		{
			$c->getDepartment();	

			$checked = ($c->getOverrideFeed() == TRUE) ? "CHECKED" : "";
			$rowClass = ($rowNumber % 2) ? "evenRow" : "oddRow\n";


			print("<tr class=\"$rowClass\">\n");
			print("<td style=\"text-align:center;\">".$c->displayCourseNo().$c->getSection() . "</td>");
			print("<td style=\"text-align:center;\">" . $c->getName() . "</td>");
			print("<td style=\"text-align:center;\"><input type=\"text\" id=\"{$return_variable}_{$rowNumber}_reg\" name=\"".$return_variable."[" . $c->getCourseAliasID() ."][registrar_key]\" value=\"". $c->getRegistrarKey() ."\" size=\"60\" maxlength=\"255\"/></td>\n");
			print("<td style=\"text-align:center;\"><label for=\"override\">Override Feed<input type=\"checkbox\" id=\"{$return_variable}_{$rowNumber}_override\" name=\"".$return_variable."[" . $c->getCourseAliasID() ."][override_feed]\"  $checked value=\"true\"\"></td>\n");


			print("</tr>\n");
			$rowNumber++;
		}
		
		print "<tr><td>&nbsp;</td></tr>";
		print "<tr><td colspan=\"3\" align=\"center\"><input type=\"submit\" value=\"Update\"> <input type=\"button\" value=\"Cancel\" onClick=\"window.location.href='index.php?cmd=admin';\"></td></tr>\n";
		
		print "</table>\n";
		
		print("</form>\n");
?>
		<script type="text/javascript" charset="utf-8">
    
				    var checkboxes = $$("#select_class input[type=checkbox]");
				    var cbControl = $("overrideAll");
    
				    cbControl.observe("click", function(){
				     checkboxes.each(function(box){
				     box.checked = cbControl.checked;
				     });
			    });
    
 		</script>
<?php

	}	
	
	
	function displayEditTerm($function, $terms, $edit_id = '')
	{	
		global $calendar, $g_terms;
		?>
		<script language="JavaScript1.2" src="public/javascript/liveSearch.js"></script>
		<script language="JavaScript1.2">
			function termReturnAction(term_array)
			{
				eval("var term = " + decode64(term_array));	
					
				document.getElementById('editTermArea').style.display = '';	

				if (typeof(term['term_id']) != "undefined")
				{
					document.getElementById('term_id').value 	= term['term_id'];					
															
					document.getElementById('sort_order').enabled = true;
					document.getElementById('term_name').enabled = true;
					document.getElementById('term_year').enabled = true;
					document.getElementById('begin_date').enabled = true;					
					document.getElementById('end_date').enabled = true;
					
					document.getElementById('term_name').focus();
				}
				
			}
			
			function checkSubmit()
			{
				if (document.getElementById('term_name').SelectIndex == -1)
				{
					alert ("Term Name is required.");
					return false;
				}
								
				if (document.getElementById('term_year').value == "")
				{
					alert ("Year is required.");
					return false;
				}

				if (document.getElementById('begin_date').value == "")
				{
					alert ("Begin Date is required.");
					return false;
				}				
				if (document.getElementById('end_date').value == "")
				{
					alert ("End Date is required.");
					return false;
				}				
				if (document.getElementById('sort_order').value == "")
				{
					alert ("Sort Order is required.");
					return false;
				}												
			}
			
			function createNew(deptName){
				document.getElementById('editTermArea').style.display = '';	
				
				document.getElementById('term_id').enabled = false;
				document.getElementById('term_name').value 	= deptName;
				
				document.getElementById('term_name').focus();
			}
			
			function load()
			{
				<?php if (!is_null($edit_id) && '' != trim($edit_id)) { 
                    $term = new term($edit_id); 
?>
					document.getElementById('editTermArea').style.display = '';
					document.getElementById('term_id').value 	=  '<?php print($term->getTermID()); ?>';
					document.getElementById('term_name').value 	= '<?php print($term->getTermName()); ?>';
					document.getElementById('term_year').value 	= '<?php print($term->getTermYear()); ?>';
					document.getElementById('begin_date').value 		= '<?php print($term->getBeginDate()); ?>';
					document.getElementById('end_date').value 		= '<?php print($term->getEndDate()); ?>';
					document.getElementById('sort_order').value = '<?php print($term->getSortOrder()); ?>';
					
					document.getElementById('newTerm').style.display = 'none';
					//document.getElementById('term_id_select').style.display = 'none';
				<?php } ?>
			}
		</script>			
		
		<table>
			<tr>
				<td width="5"></td>
				<td><input type="button" onClick="createNew('');" value="New Term" id="newTerm"></td>
			</tr>
			<tr><td width="5"></td><td class="strong">Select a Term:</td></tr>
		<form method="POST" action="index.php?cmd=admin&function=editTerms">	
			<tr><td width="5"></td>
			
				<td>
					<select id="term_id_select" name="term_id_select" onChange="this.form.submit();">
						<option value=''>Select A Term</option>
						<?php foreach ($terms as $term) { ?>
							<option value="<?php print($term->getTermID()); ?>"> <?php print($term->getTermName()); ?> <?php print($term->getTermYear()); ?>
						<?php } ?>
					</select>
				</td>
		
			</tr>
		</form>
		<form method="POST" action="index.php?cmd=admin&function=saveTerm" onSubmit="return checkSubmit();">
		<input type="hidden" id="term_id" name="term_id" value="">			
			<?php $last_term = reset($terms); ?>
			
			<tr><td></td><td>&nbsp;</td></tr>

			<table id="editTermArea" style="display: none;">
				<tr>
					<td>&nbsp;</td>
					<td>Term:</td>
									
					<td align="left">
						<select name="term_name" id="term_name">
							<?php foreach ($g_terms as $term) { ?>
								<option><?php print($term); ?></option>
							<?php } ?>
						</select>
						<input id="term_year" name="term_year" value="<?php print($last_term->getTermYear() + 1); ?>" size="4">
					</td>
				</tr>				
				<tr>
					<td>&nbsp;</td>
					<td>Begin Date:</td>
					<td>
						<input type="text" id="begin_date" name="begin_date" size="10" maxlength="10" value="<?php print($last_term->getEndDate()); ?>"/>
						<?php print($calendar->getWidgetAndTrigger('begin_date', $last_term->getEndDate())); ?>
					</td>
					<td><i>default date for class/reserve activation if not provided in feed</i></td>
				</tr>
				<tr>
					<td>&nbsp;</td>
					<td>End Date:</td>
					<td>
						<input type="text" id="end_date" name="end_date" size="10" maxlength="10" value="<?php print($last_term->getEndDate()); ?>" />
						<?php print($calendar->getWidgetAndTrigger('end_date', $last_term->getEndDate())); ?>
					</td>
					<td><i>default date for class/reserve expiration if not provided in feed</i></td>
				</tr>	
				<tr>
					<td>&nbsp;</td>
					<td>Sort Order:</td>
					<td><input type="text" id="sort_order" name="sort_order" size="4" maxlength="3" value="<?php print(intval($last_term->getSortOrder()) + 1); ?>" /> </td>
					<td><i><?php print($last_term->getSortOrder()); ?> is the current max sort.  You probably want to use the next value</i></td>
				</tr>			
			</table>
			
		</table>		
		
		<tr><td>&nbsp;</td><td>&nbsp;</td></tr>
		<tr><td>&nbsp;</td><td><input type=submit value="Save Term" id="frmSubmit"></td></tr>
		
		</form>
		<script language="JavaScript">load();</script>
		<?php
	}	
	
	function displayEditNews($function, $news, $news_item=null)
	{
		global $g_permission;
		$perms = array_flip($g_permission);
		
		$calendar = new Calendar();
		$calendar->set_option('ifFormat', '%Y-%m-%d %H:%M');
		$calendar->set_option('showsTime', true);	
		$calendar->set_option('range', array(date('Y'), (date('Y')+5)));

		?>
		<script language="JavaScript">
			function disable_dateSelect(self, id)
			{
				document.getElementById(id).enabled = false;
			}
			
			function checkSubmit()
			{
				
				if (!(document.getElementById('permission_level_0').checked ||
					  document.getElementById('permission_level_1').checked ||
					  document.getElementById('permission_level_2').checked ||
					  document.getElementById('permission_level_3').checked ||
					  document.getElementById('permission_level_4').checked ||
					  document.getElementById('permission_level_5').checked))
				{
					alert("You must select at least one Show To group.");
					return false;	  	
				}
				
				if (document.getElementById('font_class').selectedIndex == -1)
				{
					alert("Please select the desired CSS Class");
					return false;
				}
				if ((document.getElementById('begin_time').value == '' && document.getElementById('begin_time_null').checked == false))
				{
					alert("Please set the Begin date/time or check Ongoing");
					return false;
				}
				if ((document.getElementById('end_time').value == '' && document.getElementById('end_time_null').checked == false))
				{
					alert("Please set the End date/time or check Ongoing");
					return false;
				}												
			}
			
		</script>
		
		<form id="insertNews" name="insertNews" action="index.php" onsubmit="return checkSubmit()" method="Post">
		<input type="hidden" name="cmd" value="admin"/>
		<?php if (is_null($news_item)) { ?>
			<input type="hidden" name="function" value="insertNews"/>			
		<?php } else { ?>
			<input type="hidden" name="function" value="updateNews"/>			
			<input type="hidden" name="news_id" id="news_id" value="<?php print($news_item['id']); ?>" />					
		<?php } ?>
		<h2>Add News</h2>
		<table>
			<tr>
				<td valign="top">Show To:</td>
					<?php if (is_null($news_item)) { ?>
						<td>
						<?php $i = 0; 
                        foreach ($g_permission as $label => $value) { if($value >= 0) { ?>						
							<label><input type="checkbox" name="permission_level[]" id="permission_level_<?php print($i); ?>" value="<?php print($value); ?>"/>
							<?php print(strtoupper($label)); 
                            ?>
                            </label>
                            <?php
							if ($i % 2){
                            ?>
                            <br/>
                            <?php } 
                            $i++; 
                        } } ?>
						</td>
						<td valign="top"></td>
					<?php } else { //Dont allow edit of display level
							echo "<td>";
							if (is_null($news_item['permission_level'])) { 
								echo "ALL"; 
							} else { 
								echo strtoupper($perms[$news_item['permission_level']]);
							}
							echo "</td>";
					   } ?>
				</td>				
			</tr>
			
			<tr>
				<td>Style:</td>
				<?php
					$styleTypes = array('notice','emergency');
					foreach($styleTypes as $styleType)
					if (!is_null($news_item))
					{
						$selected = $news_item['class'];
						$$styleType = ($selected == $styleType) ? 'selected="selected"' : '';
					} else {
						$$styleType = '';
					}
				?>
				<td>
					<select id="font_class" name="font_class">
						<option value="notice" <?php print($notice); ?>>Notice</option>
						<option value="emergency" <?php print($emergency); ?> >Emergency</option>						
					</select>				
				</td>
			</tr>
			
			<tr>
				<?php 
					if (is_null($news_item) || is_null($news_item['begin_time']))
					{
						$begin_null =  "checked";
						$begin = date("Y-m-d h:i");
					} else {
						$begin_null = '';
						$begin = $news_item['begin_time'];
					}					
				?>
				<td>Begin:</td>
				<td>					
					<input type="text" id="begin_time" name="begin_time"  value="<?php print($begin); ?>"/>
                     (YYYY-MM-DD 24H:MIN)
					<?php print($calendar->getWidgetAndTrigger('begin_time', $begin)); ?>
					<?php
					/* allows setting begin date to null
					<input type="checkbox" id="begin_time_null" name="begin_time_null" <?php print($begin_null); ?> onclick="disable_dateSelect(this, 'begin_time');">
						Ongoing
					*/
					?>
				</td>
           	</tr>
			<tr>
				<?php
					if (is_null($news_item) || is_null($news_item['end_time']))
					{
						$end_null =  "checked";
						$end = '';
					} else {
						$end_null = '';
						$end = $news_item['end_time'];
					}					
				?>			
				<td>End:</td>
				<td>						
					<input type="text" id="end_time" name="end_time" value="<?php print($end); ?>" />
                    (YYYY-MM-DD 24H:MIN)
					<?php print($calendar->getWidgetAndTrigger('end_time', $end)); ?>
					<input type="checkbox" id="end_time_null" name="end_time_null" <?php print($end_null); ?> onclick="disable_dateSelect(this, 'end_time');">
						Ongoing
				</td>
			</tr>			
			<?php $item_sort = (is_null($news_item['sort_order'])) ? 1 : $news_item['sort_order']; ?>
			<tr>
				<td>Sort:</td>
				<td><input type="text" name="sort_order" id="sort_order" maxlength="3" size="4" value="<?php print($item_sort); ?>"/></td>
			</tr>			
			<tr>
				<td valign="top">Message Text:</td>
				<td colspan="2"><textarea name="news_text" id="news_text" wrap="virtual" cols="80" rows="6"><?php print($news_item['text']); ?></textarea></td>
			</tr>
			
			<?php $butText = (is_null($news_item)) ? "Create New" : "Submit Changes"; ?>
			<tr><td colspan="3" align="left"><input type="submit" value="<?php print($butText); ?>" /></td></tr>
		</table>
		</form>
		
		
		<br/><br/>
				
		<h2>Edit News</h2>
		<table width="100%" border="0" cellspacing="0" cellpadding="5" class="displayList">
			<tr>
				<th></th>
				<th>User Level</th>
				<th>Begin</th>
				<th>End</th>
				<th>Text</th>
			</tr>
			
			<?php 
			$rowClass = 'evenRow';
			foreach ($news as $n) { 
                $p = (!is_null($n['permission_level'])) ? strtoupper($perms[$n['permission_level']]) : 'ALL'; 
                $b = (!is_null($n['begin_time'])) ? $n['begin_time'] : 'Ongoing'; 
                $e = (!is_null($n['end_time']))   ? $n['end_time']   : 'Ongoing'; 
                $rowClass = ($rowClass=='evenRow') ? 'oddRow' : 'evenRow'; ?>
			
			<tr class="<?php print($rowClass); ?>">
				<td>
					<a href="index.php?cmd=admin&function=editNews&id=<?php print($n['id']); ?>">
						<img src="public/images/pencil.gif" alt="edit" width="24" height="20" border="0"/> Edit
				  	</a>
				</td>
				<td align="center"><?php print($p); ?></td>
				<td align="center" nowrap><?php print($b); ?></td>
				<td align="center" nowrap><?php print($e); ?></td>
				<td><?php print(htmlentities(substr($n['text'], 0, 200))); ?></td>
			</tr>
			<?php } ?>
		</table>

		<?php
	}
	
	/**
	 * @return void
	 * @desc Displays the screen to test Cisco MXE integration.
	 */
	function displayMxeInterface($args) {
		global $g_siteURL;
		$model = (
			is_array($args) && array_key_exists('model', $args)
			? $args['model']
			: array()
		);
		$model['rdUrl'] = $g_siteURL;
		$model['messages'] = array();
		$pathToMessages = '/displayers/views/admin/messages/';
		$listOfMessages = scandir(APPLICATION_PATH . $pathToMessages);
		foreach ($listOfMessages as $filePath) {
			if ('.' != $filePath 
				&& '..' != $filePath 
				&& !is_dir(APPLICATION_PATH . $pathToMessages. $filePath)
			) {
				$message = new stdClass();
				$message->target = str_replace('_', '/', substr($filePath, 0, strpos($filePath, '.xml')));
				$message->target = Filter_DelimiterExpand::filter($message->target, '-','{','}');
				$domDoc = new DOMDocument();
				$resultDoc = $domDoc->loadXML(
					file_get_contents(APPLICATION_PATH . $pathToMessages. $filePath)//,
					//- LIBXML_NOEMPTYTAG
				);
				if ($resultDoc) {
					$xpath = new DOMXPath($domDoc);
					$rootNode = $xpath->query('/message[1]')->item(0);
					$previewNode = $xpath->query('/message/preview[1]')->item(0);
					$templateNode = $xpath->query('/message/template[1]')->item(0);
					$rootNode->removeChild($previewNode);
					$rootNode->removeChild($templateNode);
					$resultNodeSource = $domDoc->saveXML($previewNode);
					$templateNodeSource = $domDoc->saveXML($templateNode,
					- LIBXML_NOEMPTYTAG);
					$message->preview = substr(
						$resultNodeSource, 
						strlen('<preview>') , 
						strrpos($resultNodeSource,'</preview>') - strlen('<preview>')
					);
					$message->template = str_replace('{{xmlDecl}}', htmlentities('<?xml version="1.0" encoding="utf-8"?>'), trim(htmlentities(substr(
						$templateNodeSource, 
						strlen('<template>'), 
						strrpos($templateNodeSource,'</template>') - strlen('<template>')
					))));
					$model['messages'][] = $message;
				}
			}
			
		}
		$this->display('mxeInterface', $model);
	}
	
	/**
	 * @return void
	 * @desc Displays the javascript for testing Cisco MXE integration.
	 */
	function displayMxeScripts() {
		$this->printStatic('mxeInterface.js');
	}
	
	/**
	 * @return void
	 * @desc Displays the javascript for testing Cisco MXE integration.
	 */
	function displayhtmlScripts() {
		$this->printStatic('htmlFunctions.js');
	}
	
	function displaySwitchUser($newUser)
	{
		$model = array(
			'newUser' => $newUser
		);
		if ('' != trim(Rd_Layout::getMessage('actionResults'))) {
			$model['msg'] = Rd_Layout::getMessage('actionResults');
		}
		$this->display('switchUser', $model);
	}
	
	function displaySelectScript($scripts= array())
	{
		$model = array('scripts' => $scripts);
		if ('' != trim(Rd_Layout::getMessage('actionResults'))) {
			$model['msg'] = Rd_Layout::getMessage('actionResults');
		}
		$this->display('selectScript', $model);
	}
	
	function displayScriptResult($script)
	{
		$model = array('result' => $script->run());
		$this->display('showScript', $model);
	}
	
	function displayRequestStatusSelect($model)
	{
		$this->display('requestStatusSelect', $model);
	}
	
	function displayRequestStatusEdit($model)
	{
		$this->display('requestStatusEdit', $model);
	}
	
	function displayClearAuditSelect($model)
	{
		$this->display('clearAuditSelect', $model);
	}
}
